<!--
 * @Description: 商品详情页面组件
 -->
<template>
  <div id="details">
    <!-- 头部 -->
    <div class="page-header">
      <div class="title">
        <p>{{ productDetails.product_name }}</p>
      </div>
    </div>
    <!-- 头部END -->

    <!-- 主要内容 -->
    <div class="main">
      <!-- 左侧商品图 -->
      <div class="block">
        <img
          style="height: 560px"
          :src="$target + productDetails.product_picture"
        />
      </div>
      <!-- 左侧商品轮播图END -->

      <!-- 右侧内容区 -->
      <div class="content">
        <h1 class="name">{{ productDetails.product_name }}</h1>
        <p class="intro">{{ productDetails.product_intro }}</p>
        <p class="store">图书商城自营</p>
        <div class="price">
          <span>{{ productDetails.product_selling_price }}元</span>
          <span
            v-show="
              productDetails.product_price !=
              productDetails.product_selling_price
            "
            class="del"
            >{{ productDetails.product_price }}元</span
          >
        </div>
        <div class="pro-list">
          <span class="pro-name">{{ productDetails.product_name }}</span>
          <span class="pro-price">
            <span>{{ productDetails.product_selling_price }}元</span>
            <span
              v-show="
                productDetails.product_price !=
                productDetails.product_selling_price
              "
              class="pro-del"
              >{{ productDetails.product_price }}元</span
            >
          </span>
          <p class="price-sum">
            总计 : {{ productDetails.product_selling_price }}元
          </p>
        </div>
        <!-- 内容区底部按钮 -->
        <div class="button">
          <el-button class="shop-cart" :disabled="dis" @click="addShoppingCart"
            >加入购物车</el-button
          >
          <el-button
            class="like"
            @click="removeCollect"
            v-if="productDetails.isCollected"
            >取消收藏</el-button
          >
          <el-button class="like" @click="addCollect" v-else>收藏</el-button>
        </div>
        <!-- 内容区底部按钮END -->
        <div class="pro-policy">
          <ul>
            <li><i class="el-icon-circle-check"></i> 图书商城自营</li>
            <li><i class="el-icon-circle-check"></i> 图书商城发货</li>
            <li><i class="el-icon-circle-check"></i> 7天无理由退货</li>
            <li><i class="el-icon-circle-check"></i> 7天价格保护</li>
          </ul>
        </div>
      </div>
      <!-- 右侧内容区END -->
    </div>
    <!-- 主要内容END -->
  </div>
</template>
<script>
import { mapActions } from "vuex";
export default {
  data() {
    return {
      dis: false, // 控制“加入购物车按钮是否可用”
      productID: "", // 商品id
      productDetails: "", // 商品详细信息
      productPicture: "", // 商品图片
    };
  },
  // 通过路由获取商品id
  activated() {
    if (this.$route.query.productID != undefined) {
      this.productID = this.$route.query.productID;
    }
  },
  watch: {
    // 监听商品id的变化，请求后端获取商品数据
    productID: function (val) {
      this.getDetails(val);
      this.getDetailsPicture(val);
    },
  },
  methods: {
    ...mapActions(["unshiftShoppingCart", "addShoppingCartNum"]),
    // 获取商品详细信息
    getDetails(val) {
      this.$axios
        .post("/api/product/getDetails", {
          productID: val,
          user_id: this.$store.getters.getUser.user_id,
        })
        .then((res) => {
          this.productDetails = res.data.Product[0];
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 获取商品图片
    getDetailsPicture(val) {
      this.$axios
        .post("/api/product/getDetailsPicture", {
          productID: val,
        })
        .then((res) => {
          this.productPicture = res.data.ProductPicture;
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    // 加入购物车
    addShoppingCart() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      this.$axios
        .post("/api/user/shoppingCart/addShoppingCart", {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID,
        })
        .then((res) => {
          switch (res.data.code) {
            case "001":
              // 新加入购物车成功
              this.unshiftShoppingCart(res.data.shoppingCartData[0]);
              this.notifySucceed(res.data.msg);
              break;
            case "002":
              // 该商品已经在购物车，数量+1
              this.addShoppingCartNum(this.productID);
              this.notifySucceed(res.data.msg);
              break;
            case "003":
              // 商品数量达到限购数量
              this.dis = true;
              this.notifyError(res.data.msg);
              break;
            default:
              this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    removeCollect() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      this.$axios
        .post("/api/user/collect/deleteCollect", {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID,
        })
        .then((res) => {
          if (res.data.code == "001") {
            this.getDetails(this.$route.query.productID);
            // 取消收藏成功
            this.notifySucceed(res.data.msg);
          } else {
            // 取消收藏失败
            this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
    addCollect() {
      // 判断是否登录,没有登录则显示登录组件
      if (!this.$store.getters.getUser) {
        this.$store.dispatch("setShowLogin", true);
        return;
      }
      this.$axios
        .post("/api/user/collect/addCollect", {
          user_id: this.$store.getters.getUser.user_id,
          product_id: this.productID,
        })
        .then((res) => {
          if (res.data.code == "001") {
            this.getDetails(this.$route.query.productID);
            // 添加收藏成功
            this.notifySucceed(res.data.msg);
          } else {
            // 添加收藏失败
            this.notifyError(res.data.msg);
          }
        })
        .catch((err) => {
          return Promise.reject(err);
        });
    },
  },
};
</script>
<style scoped>
/* 全局样式 */
#details {
  font-family: 'Helvetica Neue', Arial, sans-serif;
  background-color: #f8f9fa;
  min-height: 100vh;
}

/* 头部样式 - 简约风格 */
.page-header {
  height: 70px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
}

.page-header .title {
  width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-header .title p {
  font-size: 20px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0;
}

/* 主要内容容器 - 现代简约风格 */
.main {
  width: 1200px;
  margin: 30px auto;
  background: #fff;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  display: flex;
  padding: 40px;
  box-sizing: border-box;
}

/* 左侧商品图 - 简洁展示 */
.block {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  border-right: 1px solid #eaeaea;
}

.block img {
  max-width: 100%;
  max-height: 480px;
  object-fit: contain;
  border-radius: 6px;
}

/* 右侧内容区 - 干净整洁的布局 */
.content {
  width: 50%;
  padding: 0 30px;
}

.content .name {
  font-size: 26px;
  font-weight: 700;
  color: #34495e;
  margin-bottom: 18px;
  line-height: 1.4;
}

.content .intro {
  color: #7f8c8d;
  font-size: 15px;
  line-height: 1.6;
  margin-bottom: 25px;
}

.content .store {
  color: #3498db;
  font-size: 15px;
  margin-bottom: 25px;
  display: flex;
  align-items: center;
  font-weight: 500;
}

.content .store::before {
  content: "";
  display: inline-block;
  width: 18px;
  height: 18px;
  background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%233498db"><path d="M12 1L3 5v6c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V5l-9-4zm-1 6h2v2h-2V7zm0 4h2v6h-2v-6z"/></svg>') no-repeat;
  margin-right: 10px;
}

/* 价格区域 - 突出但不夸张 */
.price {
  display: flex;
  align-items: baseline;
  margin: 30px 0;
  padding-bottom: 25px;
  border-bottom: 1px solid #ecf0f1;
}

.price span:first-child {
  font-size: 32px;
  font-weight: 700;
  color: #e74c3c;
}

.price .del {
  font-size: 18px;
  color: #95a5a6;
  text-decoration: line-through;
  margin-left: 15px;
}

/* 商品清单 - 简洁卡片 */
.pro-list {
  background: #f9f9f9;
  border-radius: 10px;
  padding: 25px;
  margin: 35px 0;
  border: 1px solid #eee;
}

.pro-list .pro-name {
  display: block;
  font-size: 16px;
  margin-bottom: 12px;
  color: #2c3e50;
  font-weight: 500;
}

.pro-list .pro-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pro-list .pro-price span:first-child {
  font-size: 20px;
  color: #e74c3c;
  font-weight: 600;
}

.pro-list .pro-del {
  font-size: 16px;
  color: #95a5a6;
  text-decoration: line-through;
}

.pro-list .price-sum {
  font-size: 20px;
  color: #2c3e50;
  font-weight: 700;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px dashed #ddd;
  text-align: right;
}

/* 按钮区域 - 现代扁平化设计 */
.button {
  display: flex;
  gap: 20px;
  margin: 40px 0;
}

.button .el-button {
  height: 52px;
  font-size: 17px;
  border-radius: 6px;
  flex: 1;
  transition: all 0.2s;
  font-weight: 500;
  letter-spacing: 0.5px;
}

.button .shop-cart {
  background: #e74c3c;
  border: none;
  color: white;
}

.button .shop-cart:hover {
  background: #c0392b;
  transform: translateY(-2px);
}

.button .shop-cart:active {
  transform: translateY(0);
}

.button .like {
  background: #fff;
  border: 2px solid #3498db;
  color: #3498db;
}

.button .like:hover {
  background: #f8f9fa;
  border-color: #2980b9;
  color: #2980b9;
}

/* 服务政策 - 简约排列 */
.pro-policy ul {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.pro-policy li {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #7f8c8d;
  font-weight: 500;
}

.pro-policy i {
  color: #2ecc71;
  margin-right: 8px;
  font-size: 16px;
}

/* 响应式调整 */
@media (max-width: 1240px) {
  .main, .page-header .title {
    width: 95%;
  }
}

@media (max-width: 768px) {
  .main {
    flex-direction: column;
    padding: 25px;
  }
  
  .block, .content {
    width: 100%;
    padding: 0;
  }
  
  .block {
    border-right: none;
    border-bottom: 1px solid #eee;
    margin-bottom: 25px;
    padding-bottom: 25px;
  }
  
  .button {
    flex-direction: column;
    gap: 15px;
  }
  
  .button .el-button {
    width: 100%;
  }
}
</style>